package tongwentongshu.com.app.activity;

import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.adapter.DepositAdapter;
import tongwentongshu.com.app.adapter.DoudouAdapter;
import tongwentongshu.com.app.adapter.NullAdapter;
import tongwentongshu.com.app.bean.DouBean;
import tongwentongshu.com.app.bean.ShareDetailsBean;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;

/**
 * Created by XiongBin on 2017/7/18.
 */

public class ShudouActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private DoudouAdapter doudouAdapter;

    RecyclerView read_people;
    private NullAdapter nullAdapter;
    protected boolean onRefreshJudge = false;
    private View baseView;
    private Map<String, String> map;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_my_share_details;
    }

    @Override
    protected int getTitleId() {
        return R.string.doudou;
    }

    @Override
    protected void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        nullAdapter = new NullAdapter();
        nullAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(nullAdapter);

    }

    @Override
    protected void initData() {
        initShareDetails();
    }

    public void getBasicView() {
        if (null == baseView) {
            baseView = getLayoutInflater().inflate(R.layout.view_shudou_head, (ViewGroup) mRecyclerView.getParent(), false);
            read_people = (RecyclerView) baseView.findViewById(R.id.ll_read_people);
            TextView refund = (TextView) baseView.findViewById(R.id.tv_refund);
            refund.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle b = new Bundle();
                    b.putString("shudou", msg.getData().getDoudou() + "书豆");
                    startActivity(RechargeActivity.class, b);
                }
            });
            TextView with = (TextView) baseView.findViewById(R.id.tv_with);
            with.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle b = new Bundle();
                    startActivity(ShudouWithdrawalsActivity.class, b);
                }
            });
            onRefreshJudge = true;
            mSwipeRefreshLayout.setOnRefreshListener(this);
            read_people.setLayoutManager(new LinearLayoutManager(mContext));
            List arr = new ArrayList<>();
            doudouAdapter = new DoudouAdapter(mContext, arr);
            doudouAdapter.setOnLoadMoreListener(this, mRecyclerView);
            doudouAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            View empty = getLayoutInflater().inflate(R.layout.empty_reader, (ViewGroup) mRecyclerView.getParent(), false);
            //pullToRefreshAdapter.setPreLoadNumber(3);
//            myShareDetailsAdapter.setEmptyView(empty);
            read_people.setAdapter(doudouAdapter);
            nullAdapter.addHeaderView(baseView);
//            mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
//                @Override
//                public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
//                    Toast.makeText(mContext, Integer.toString(position), Toast.LENGTH_SHORT).show();
//                }
//            });
        }
        TextView bookName = (TextView) baseView.findViewById(R.id.tv_shudou_sum);
        bookName.setText(msg.getData().getDoudou() + "书豆");//书豆数量

    }

    public void initShareDetails() {
        map = new ArrayMap<>();
        map.put("token", Cache.getToken());

        UpdateFractory.getBuild()
                .name("DoudouDetailCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<DouBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(DouBean myshare, String msg) {
                        onSucc(myshare);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }

    private DouBean msg;

    public void onSucc(DouBean msg) {
        this.msg = msg;
        getBasicView();
        if (msg.getData() != null && msg.getData().getDetail().size() > 0) {
            if (onRefreshJudge) {
//                Log.e(TAG, "onRefreshJudge: " + onRefreshJudge);
                doudouAdapter.setNewData(msg.getData().getDetail());
                mSwipeRefreshLayout.setRefreshing(false);
                doudouAdapter.setEnableLoadMore(true);
                onRefreshJudge = false;
            }
        } else {
            mSwipeRefreshLayout.setRefreshing(false);
            doudouAdapter.setEnableLoadMore(true);
            onRefreshJudge = false;
        }
    }


    @Override
    public void onRefresh() {
        onRefreshJudge = true;
        initShareDetails();
    }

    @Override
    public void onLoadMoreRequested() {
        doudouAdapter.loadMoreEnd(true);
    }
}
